package com.yeyks.controller;

import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import com.yeyks.common.appcommon.dto.order.AppGrabOrderDTO;
import com.yeyks.common.em.WebSocketTypeEnum;
import com.yeyks.common.result.OrderPickWebSocket;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.common.utils.apputil.redis.RedisService;
import com.yeyks.common.utils.apputil.rediskey.RedisKey;
import com.yeyks.commonReference.append.rocketmq.pojo.RocketMQWebsocketBasePojo;
import com.yeyks.commonReference.append.rocketmq.service.producer.MQProducerService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 发送消息实例
 * @author Administrator
 */
@RestController
@RequestMapping("/grad/test")
public class GradOrderTestController {


    private Integer hotelId = 2;

    private Integer userId = 3;

    @Autowired
    private RedisService redisService;

    @Autowired
    private MQProducerService mqProducerService;

    //测试给wenSocket发Mq消息
    @PostMapping("/send/socket")
    public void sendSocketMq(@RequestParam(name = "count", defaultValue = "1", required = false) Integer count){
        OrderPickWebSocket webSocket = new OrderPickWebSocket();
        webSocket.setRoomIds(Lists.newArrayList(7391,7376,7373));
        webSocket.setCheckIn(1565712000000L);
        webSocket.setPhone("15188445491");
        webSocket.setManualPickOrder(1);
        webSocket.setUserShowPrice(15000);
        webSocket.setHotelId(755);
        webSocket.setCheckOut(1565798400000L);
        webSocket.setOperatorId(2235);
        webSocket.setTimestamp(System.currentTimeMillis());

        for(int i = 0; i < count; i++){
            webSocket.setUserId(263+i);
            RocketMQWebsocketBasePojo pojo = new RocketMQWebsocketBasePojo();
            pojo.setType(WebSocketTypeEnum.ROB_ORDER.value());
            pojo.setUserId(webSocket.getOperatorId());
            pojo.setBody(JSONUtil.toJsonStr(webSocket));
            mqProducerService.sendRobToBMsg(pojo);
        }
    }


    //测试redis的存放和获取hash集合

    @PostMapping("/set/hash")
    public void setGrabHash(){


        List<Map> hashMap = Lists.newArrayList();
        //hotelId ++;
        userId ++;
        AppGrabOrderDTO appGrabOrderDTO =createTestGrabOrder(hotelId,userId);

        redisService.saveHashString(ObjectConvertUtil.objectToMap(appGrabOrderDTO), RedisKey.KEY+hotelId,
                RedisKey.HASH_KEY+userId, 30, TimeUnit.MINUTES);

    }

    @PostMapping("/get/hash")
    public List<AppGrabOrderDTO> getGrabHashList(){
        List<AppGrabOrderDTO> grabOrderDTOList = Lists.newArrayList();
        List<Map> grabMap = redisService.getList((RedisKey.KEY+hotelId));

        for(Map<String, Object> map : grabMap){
            try {
                AppGrabOrderDTO grabOrderDTO = ObjectConvertUtil.mapToBean(map, AppGrabOrderDTO.class);
                //验证时间是否超时
                if((System.currentTimeMillis()-grabOrderDTO.getStartTime().getTime())/1000 > RedisKey.KEEP_TIME){
                    //删除超时的数据
                    redisService.deleteHashString((RedisKey.KEY+hotelId),(RedisKey.HASH_KEY+grabOrderDTO.getUserId()));
                    continue;
                }
                grabOrderDTOList.add(grabOrderDTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return  grabOrderDTOList;
    }

    private AppGrabOrderDTO createTestGrabOrder(Integer hotelId, Integer userId){
        AppGrabOrderDTO appGrabOrderDTO = new AppGrabOrderDTO();
        appGrabOrderDTO.setHotelId(hotelId);
        appGrabOrderDTO.setUserId(userId);
        appGrabOrderDTO.setKeepTime(RedisKey.KEEP_TIME);
        appGrabOrderDTO.setStartTime(new Date());
        appGrabOrderDTO.setTimestamp(1565588904000L);
        appGrabOrderDTO.setManualPickOrder(1);
        appGrabOrderDTO.setOperatorId(3395);
        appGrabOrderDTO.setPhone("18582131913");
        return appGrabOrderDTO;
    }

}
